﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using OpenGL4NET;
using WSCore;

namespace WSTest
{
	public partial class Form1 : Form
	{
		private WSTexture2D _texture;
		public Form1()
		{
			InitializeComponent();
		}

		public void MyInit()
		{
			using (Bitmap bitmap = new Bitmap(@"c:\actor.png"))
			{
				_texture = new WSTexture2D(bitmap);
			}
		}

		protected override void WndProc(ref Message m)
		{
			switch (m.Msg)
			{
 				case Windows.WM_PAINT:
				case 0x14:
					Draw();
					break;
				default: 
					base.WndProc(ref m);
					break;
			}
			
		}

		private void Draw()
		{
			gl.Clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
			gl.LoadIdentity();

			gl.Translatef(0f, 0f, -6.0f);

			gl.Begin(GL.QUADS);
			gl.Color3f(1f, 1f, 1f);
			_texture.Use();
			gl.TexCoord2f(0f, 0f);
			gl.Vertex3f(-1.0f, 1.0f, 0.0f);
			gl.TexCoord2f(1f, 0f); 
			gl.Vertex3f(1.0f, 1.0f, 0.0f);
			gl.TexCoord2f(1f, 1f);
			gl.Vertex3f(1.0f, -1.0f, 0.0f);
			gl.TexCoord2f(0f, 1f); 
			gl.Vertex3f(-1.0f, -1.0f, 0.0f);
			gl.End();								

			wgl.SwapBuffers(Windows.GetDC(this.Handle));
		}

		protected override void OnSizeChanged(EventArgs e)
		{
			gl.Viewport(0, 0, ClientSize.Width, ClientSize.Height);
			gl.MatrixMode(GL.PROJECTION);
			gl.LoadIdentity();
			//var pml = gl.GetUniformLocation((uint)Program.ID, "projectionMatrix");
			//Matrixf m = Matrixf.Perspective(45f, (float)ClientSize.Width / ClientSize.Height, 0.1f, 100f);
			//gl.UniformMatrix4fv(pml, 1, false, ref m);

			Program.gluPerspective(45f, (float)ClientSize.Width / ClientSize.Height, 0.1f, 100f);

			gl.MatrixMode(GL.MODELVIEW);
			gl.LoadIdentity();
		}
	}
}
